package com.aktk.battlenole;
import java.util.ArrayList;

import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class BoardManager implements BoardCell.OnClickListener 
{
	private ArrayList<BoardCell> cells;
	private int rows, columns;
	public BoardManager (int rows, int columns)
	{
		this.rows = rows;
		this.columns = columns;
		cells = new ArrayList<BoardCell> (rows * columns);
	}
	
	public void Draw (Activity parent, LinearLayout host)
	{
        for (int row = 0; row < rows; ++row)
        {
        	LinearLayout rowLayout;
        	if (row == 0)
        	{
        		 rowLayout = (LinearLayout) parent.getLayoutInflater().inflate(R.layout.top_row_container, host, false);        		
        	}
        	else if (row == rows - 1)
        	{
        		rowLayout = (LinearLayout) parent.getLayoutInflater().inflate(R.layout.bottom_row_container, host, false);        		
        	}
        	else
        	{
        		rowLayout = (LinearLayout) parent.getLayoutInflater().inflate(R.layout.row_container, host, false);        		
        	}
        	host.addView(rowLayout);
        	for (int column = 0; column < columns; ++column)
        	{
        		BoardCell cell = new BoardCell(row, column);
        		cells.add(cell);
        		cell.Draw(rowLayout);
        		cell.setOnClickListener(this);
        	}
        }
	}
	
	public ArrayList<BoardCellState> getStates()
	{
		int cellCount = rows * columns;
		ArrayList<BoardCellState> states = new ArrayList<BoardCellState> (cellCount);
		for (int index = 0; index < cellCount; ++index)
		{
			BoardCell cell = cells.get(index);
			BoardCellState current = cell.getCurrentState();
			BoardCellState copy = current.Copy();
			states.add(copy);
		}
		return states;
	}
	
	public void setStatus (ArrayList<BoardCellState> states)
	{
		for (int index = 0; index < states.size(); ++index)
		{
			cells.get(index).setCurrentState(states.get(index));
		}
	}
	
	private OnClickListener listener;
	public void setOnClickListener (OnClickListener listener)
	{
		this.listener = listener;
	}
	
	@Override
	public void onClick(BoardCell cell) 
	{
		if (listener != null)
		{
			listener.onClick(cell.getRow(), cell.getColumn());
		}
	}
	
	public interface OnClickListener
	{
		void onClick (int row, int column);
	}
}
